import "../lib/ramda.js"
import "../lib/jquery.js"
import utils from "../lib/utils.js"

const FPS = 50

const APPS = [
    {
        id: "fflash",
        name: "FFlash",
        title:[
            "Black-White Chess AI",
            "15x15 黑白棋(UCT-AI)",
        ],
    },
    {
        id: "sslash",
        name: "SSlash",
        title:[
            "Black-White Chess AI",
            "10x10 黑白棋(UCT-AI)",
        ],
    },
    {
        id: "shogia",
        name: "Shogi A",
        title:[
            "Shogia Chess AI",
            "将棋对弈(AI)",
        ],
    },
    {
        id: "vtexted",
        name: "Vtexted",
        title:[
            "Editor for ↓← Style",
            "竖向文本编辑器",
        ],
    },
    {
        id: "lianstar",
        name: "连星",
        title:[
            "Renju RIF & AI",
            "五子棋AI对弈",
        ],
    },
    {
        id: "canglin",
        name: "Canglin(沧璘)",
        title: [
            "An try for Typed-Scheme",
            "类型化Scheme语言试验",
        ],
    },
    {
        id: "thexasm",
        name: "Thexasm",
        title: [
            "Thex-Assembly Simulate",
            "新汇编指令集的模拟器",
        ],
    },
]

const Tools = {
    url_pack(id){
        return `./module/${id}/index.html?t=${utils.clock.now_second()}`
    },
}

const ux = {
    time:{
        dom: $("#main #head #time"),
        opacity_way: -1,
        opacity_step: 0.01,
    },
    box:{
        dom: $("#main #box"),
        list:{
            dom: $("#main #box #list"),
        },
    },
    template:(id)=>$(`ux-template#${id}`),
}

const render = {
    time(){
        const opacity = Number(ux.time.dom.css("opacity"))
        if (opacity < 0.1){
            ux.time.opacity_way = 1
        }else if (opacity >= 1.0){
            ux.time.opacity_way = -1
        }
        ux.time.dom.css("opacity", (opacity + (ux.time.opacity_step * ux.time.opacity_way)).toString())
        const now = (new Date()).toLocaleString()
        ux.time.dom.html(now)
        return this
    },
    box(){
        function title_html(lines){
            return lines.reduce((html, line)=>`${html}${line}<br/>`, "")
        }
        ux.box.list.dom.children().remove()
        const template = ux.template("app-item")
        let orders = utils.redis.array["-i-orders"].map(Number)
        for (const i of orders){
            const appitem = APPS[i]
            if (appitem["_hide"] == undefined){
                template.find("#name").html(appitem.name).attr("href", Tools.url_pack(appitem.id))
                template.find("#title").html(title_html(appitem.title))
                ux.box.list.dom.append(template.html())
                const dom = ux.box.list.dom.children().last()
                dom.click(function(){
                    orders = [i, ...(orders.filter((a)=>(a != i)))]
                    utils.redis.array["-i-orders"] = orders
                    render.box()
                })
            }
        }
    },
}

function frame(){
    render.time()
}

function init(){
    function default_orders() {
        const orders = APPS.map((_,i)=>(i))
        return String(orders)
    }
    utils.redis.init()
    utils.redis.Event.on_key_notfound = (k)=>{
        const k_maps = {
            "-i-orders": default_orders(),
        }
        return k_maps[k]
    }
}

function main(args){
    if (args.length > 0){
        const order = Number(args[0])
        const id = (order >= 0 && order < APPS.length) ? APPS[order].id : (function(){
            const matched = APPS.filter((a)=>(a.id == args[0]))
            if (matched.length > 0) {return matched[0].id} else {return undefined}
        }())
        if (id != undefined){
            return window.location.replace(Tools.url_pack(id))
        }
    }
    init()
    utils.clock.frame_watch(frame).start(FPS)
    render.box()
    $(document.body).show()
}

main(((window.location.search.length > 2) ? [window.location.search.slice(2)] : []))

console.log("Fuck WASM")
